package cn.biplam.back.bean.ddl.sql2db.field;

import cn.biplam.back.bean.ddl.sql2db.TypeParser;
import cn.biplam.back.sql.ddl.Field;
import cn.biplam.back.sql.ddl.Table;
import cn.biplam.back.sql.type.Type;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;

public class FieldCommen {
	
	/**
	 * 返回表创建语句
	 * @return 建表SQL语句
	 * @throws ErrorMessage 
	 */
	public String get(Table table) throws ErrorMessage{
		
		KvMap<Integer, Field> fields = table.getField();
		StringBuilder sb = new StringBuilder();
		StringBuilder sbComment = new StringBuilder();
		if(fields.size()>0){
			sb.append(" drop table "+table.getPhysicalName()+";\n\t");
		}
		/**字段信息*/
		for(int i=0;i<fields.size();i++){
			if(i==0){
				sb.append(" CREATE TABLE ");
				sb.append(" "+table.getPhysicalName());
				sb.append(" (\n\t");
				sb.append(" "+getFieldExp(fields.getByKey(i)));
			}else{
				sb.append(" ,\n\t");
				sb.append(" "+getFieldExp(fields.getByKey(i)));
			}
			sbComment.append(getFieldCommentOracle(fields.getByKey(i),table));
		}
		sb.append(" );\n\t");
		/**common无法跨数据库*/
//		/**说明信息*/
//		if(fields.size()>0){
//			String tableComment = "";
//			if(table.getComment()!=null){
//				tableComment = table.getComment();
//				sb.append(" comment on table "+table.getPhysicalName()+" is '"+tableComment+"';\n\t");
//			}
//			if(sbComment.toString()!=null && !"null".equals(sbComment.toString().trim()) 
//					 && !"".equals(sbComment.toString().trim())){
//			    sb.append(sbComment.toString());
//			}
//		}
//
		return sb.toString();

	}
	
	/**
	 * 功能   得到oracle数据库表创建语句
	 * @return
	 */
	public String getDrop(Table table){
		KvMap<Integer, Field> fields = table.getField();
		StringBuilder sb = new StringBuilder();
		
		for(int i=0;i<fields.size();i++){
			sb.append(this.getFieldDropOracle(fields.getByKey(i),table)+"\n");
		}

		return sb.toString();
	}
	
	/**
	 * 功能	  oracle数据库删除表列sql
	 * @param mField
	 * @return
	 */
	private String getFieldDropOracle(Field mField,Table table){
		return " alter table "+table.getPhysicalName()+" drop column "+mField.getPhisicalName()+";\n";
	}
	
	/**
	 * 取字段表达式
	 * @param field
	 * @return
	 * @throws ErrorMessage 
	 */
	private String getFieldExp(Field field) throws ErrorMessage{
		
		StringBuilder sb =new StringBuilder();

		/**名称转为大写*/
		sb.append(field.getPhisicalName());

		/**确保字段名称长度在24个字符以内的，其类型描述能对齐*/
		for(int i=field.getPhisicalName().length();i<32;i+=8){
			sb.append("\t");
		}

		sb.append(new TypeParser((Type)field.getType(),getDbType()).get());

		/**Default语句*/
		if(field.getDefault()!=null){
			sb.append(" DEFAULT ");
			sb.append(field.getDefault().toString());
		}

		/**Not Null语句*/
		if(!field.isNull()){
			sb.append(" NOT NULL ");
		}
		
		return sb.toString();
	}
	
	private String getFieldCommentOracle(Field mField,Table table){
		String columnComment = "";
		if(mField.getComment()!=null){
			columnComment = mField.getComment();
			columnComment = " comment on column "+table.getPhysicalName()+"."+mField.getPhisicalName()+" is '"+columnComment+"';\n";
		}
		return columnComment;
	}
	
	/**
	 * 返回数据库类型
	 * @return
	 */
	public String getDbType(){
		return "1";
	}
}
